package cn.wy.system.controller;

import cn.wy.basic.util.AjaxResult;
import cn.wy.basic.util.PageList;
import cn.wy.system.domain.Dictionarytype;
import cn.wy.system.query.DictionarytypeQuery;
import cn.wy.system.service.DictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping ("/dictionarytype")
public class DictionarytypeController {
    @Autowired
    private DictionarytypeService dictionaryTypeService;

    //查询全部
    @GetMapping
    public List<Dictionarytype> findAll() {
        return dictionaryTypeService.findAll();
    }
    //分页
    @PostMapping
    public PageList<Dictionarytype> pageQuery(@RequestBody DictionarytypeQuery dictionarytypeQuery) {
        return dictionaryTypeService.queryPage(dictionarytypeQuery);
    }
    //查询单个
    @GetMapping("/{id}")
    public Dictionarytype findById(@PathVariable("id") Long id) {
        return dictionaryTypeService.findOne(id);
    }
    //添加或者修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype){
        try {
            if(dictionarytype.getId() == null){
                dictionaryTypeService.insert(dictionarytype);
            }else {
                dictionaryTypeService.update(dictionarytype);
            }
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！");
        }
    }
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            dictionaryTypeService.delete(id);
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        try {
            dictionaryTypeService.patchDel(ids);
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
}
